package com.ehotting.edsta.circuitDesign.gen.element.script;

import com.ehotting.edsta.circuitDesign.domain.EsCircuitCodeDataTemplate;
import com.ehotting.edsta.circuitDesign.domain.EsRuleScript;
import com.ehotting.edsta.circuitDesign.domain.EsSceneParam;
import com.ehotting.edsta.circuitDesign.gen.ScriptConstants;

import java.util.List;

public abstract  class EqualElementScriptAbstract extends ElementScriptAbstract {


    /***
     * 找元器件，根据取值查找对应元器件，
     * 比如输入：
     * r1=200;r1.xx=300
     * r2=400;r2.yy=400
     * 找出对应值的元器件
     * @param esRuleScript
     * @param inputParamList
     * @param outputParamList
     * @return
     */
    public String invoke(EsRuleScript esRuleScript, List<EsCircuitCodeDataTemplate> inputParamList, List<EsSceneParam> outputParamList, String functionName){

        // ------------------------------------脚本组装------------------------------------
        StringBuffer buf = new StringBuffer();

        /**1、定义方法注释 **/
        buf.append("/**").append(esRuleScript.getContent()).append("**/").append(ScriptConstants.LINE);

        /**2、定义方法**/
        buf.append(functionStart( functionName, inputParamList));

        /**3、定义变量**/
        buf.append( ScriptConstants.TAB+"List<Map<String, Object>> componentList = new ArrayList();"+ ScriptConstants.LINE);

        /**4、定义程序主体**/
        buf.append( genBody(esRuleScript, outputParamList));

        /**5、返回**/
        buf.append( ScriptConstants.TAB + "return ElementReturnHelper.success();"+ ScriptConstants.LINE);


        /**6、方法结尾**/
        buf.append( "};"+ ScriptConstants.LINE);

        return  buf.toString();
    }

    /**
     * 业务逻辑实现
     * @param esRuleScript
     * @param outputParamList
     * @return
     */
    public String genBody(EsRuleScript esRuleScript, List<EsSceneParam> outputParamList){
        StringBuffer buf = new StringBuffer();
        String[] lines = esRuleScript.getContent().split(ScriptConstants.LINE);
        for(int index=0;index<lines.length;index++) {
            buf.append(genBody(lines[index], outputParamList,index));
        }
        return buf.toString();
    }

    private String genBody(String content, List<EsSceneParam> outputParamList, int index){
        StringBuffer buf = new StringBuffer();
        buf.append(ScriptConstants.TAB+"/**********************current script:"+content+"**********************/"+ ScriptConstants.LINE);
        outputParamList = findXElementList(content, outputParamList);

        // 2、定义for循环
        buf.append(this.startFor(content,outputParamList));
        int i=outputParamList.size();

        buf.append( ScriptConstants.LINE);
        // 3、if开始
        buf.append(startIf(content,ScriptConstants.TAB(i+1)));
        // 4、定义赋值
        buf.append(handleSetValue( outputParamList, i));

        // 5、if结尾
        buf.append(ScriptConstants.TAB(i+1)).append( "}"+ ScriptConstants.LINE);

        // 定义for循环的结尾
        buf.append(this.endFor(i));

        buf.append(handleResult( outputParamList,  index));

        return buf.toString();
    }


    public abstract String handleResult(List<EsSceneParam> outputParamList, int index);

}
